package com.qingke.note.command_impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.qingke.note.Console;
import com.qingke.note.IDao.LabelMapper;
import com.qingke.note.IDao.NoteMapper;
import com.qingke.note.IDao.TypeMapper;
import com.qingke.note.command.Command;
import com.qingke.note.pojo.Label;
import com.qingke.note.pojo.Note;
import com.qingke.note.pojo.Type;
import com.qingke.note.pojo.User;

public class UpdateCommand extends Command {

	public UpdateCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		if(currentUser==null){
			Console.println("您还未登录，不能执行此操作");
			return;
		}
		
		Console.println("【1】修改备忘内容");
		Console.println("【2】修改标签内容");
		int choice = Console.askInputInt("请输入操作序号");
		while(true){
			if(choice==1){
				updateNote();
			}else if(choice==2){
				updateLabel();
			}else {
				choice = Console.askInputInt("输入不正确，请重新输入");
				continue;
			}
			return;
		}
	}

	private void updateLabel() {
		SqlSession session = getSession();
		LabelMapper labelMapper = session.getMapper(LabelMapper.class);		
		List<Label> labels = labelMapper.selectAllLabels();
		
		int index =1;
		for (Label label : labels) {
			Console.print("【"+index+"】");
			Console.println(label);
			index++;
		}
		
		int choice = Console.askInputInt("请输入要修改的标签序号") - 1;
		while(true){
			if(choice>=index){
				Console.println("序号不存在");
				choice = Console.askInputInt("请重新输入要修改的标签序号");
				continue;
			}
			Label label = labels.get(choice);
			label = editLabel(label);
			labelMapper.updateByPrimaryKeySelective(label);
			session.commit();
			Console.println("标签已成功修改");
			session.close();
		}	
	}

	private Label editLabel(Label label) {
		label.setType(updateType(label));
		return label;
	}

	private Type updateType(Label label) {
		Type type = label.getType();
		Console.print("当前标签的类型为:");
		Console.println(type);
		Console.println("请问要替换为哪个类型的标签");
		return chooseType();
	}

	
	public Type chooseType(){
		Type type ;
		SqlSession session = getSession();
		TypeMapper typeMapper = session.getMapper(TypeMapper.class);
		List<Type> types = typeMapper.selectAllTypes();
		Map<String, Integer> contents = new HashMap<String, Integer>();
		int index = 1;
		for (Type type2 : types) {
			Console.print("【"+index+"】");
			Console.println(type2);
			contents.put(type2.getContent(),type2.getId());
			index++;
		}
		Console.println("【"+index+"】 新建一个标签类型");
		int choice = Console.askInputInt("请输入序号");
		while(true){
			if(choice<1||choice>index){
				choice = Console.askInputInt("序号不正确，请重新输入");
				continue;
			}else if(choice==index){
				session.close();
				return createType(contents,types);
				
			}else{
				type = types.get(choice-1);
				Console.println("已选定为"+type.getContent()+"类型");
				session.close();
				return type;
			}
		}
	}
	
	public Type createType(Map<String, Integer> contents, List<Type> types){
		SqlSession session = getSession();
		TypeMapper typeMapper = session.getMapper(TypeMapper.class);
		String typeName = Console.askInput("请输入要新建的标签类型:");
		Type newType = new Type();
		if(contents.containsKey(typeName)){
			Console.println("名为"+typeName+"的标签类型已存在，自动使用该标签");
			newType = types.get(contents.get(typeName));
		}else{
			newType.setContent(typeName);
			List<Label> labels = new ArrayList<Label>();
			newType.setLabels(labels);
			typeMapper.insert(newType);	
			session.commit();
			newType = typeMapper.selectByContent(typeName);
		}
		Console.println("新标签 ["+newType.getContent()+"] 已创建成功");
		session.close();
		return newType;
	}
	
	private void updateNote() {
		List<Note> notes = currentUser.getNotes();
		int index =1;
		for (Note note : notes) {
			Console.print("【"+index+"】");
			Console.println(note);
			index++;
		}
		
		int choice = Console.askInputInt("请输入要修改的备忘序号") - 1;
		while(true){
			if(choice<1||choice>=index){
				choice = Console.askInputInt("序号不正确，请重新输入");
				continue;
			}else{
				//修改note
				Note note = editNote(notes.get(choice));
				SqlSession session = getSession();
				NoteMapper noteMapper = session.getMapper(NoteMapper.class);
				noteMapper.updateByPrimaryKey(note);
				session.commit();
				Console.println("备忘修改成功");
				currentUser = new LoginCommand(currentUser).login();
				session.close();
			}
		}
	}

	private Note editNote(Note note) {
		// TODO Auto-generated method stub
		
		String title = Console.askInput("请输入新标题，不修改则回车");
		if(!"".equals(title.trim())){
			note.setTitle(title);
		}
		
		String content = Console.askInput("请输入新内容,不修改则回车");
		if(!"".equals(content.trim())){
			note.setContent(content);
		}
		
		String time = Console.askInput("请输入新提醒时间(年-月-日 时-分)，不修改则回车");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm");
		if(!"".equals(time.trim())){
				
			try {
				Date date = sdf.parse(time);
				note.setTime(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		return note;
	}

}
